有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何链接首选项。xml到我的表格布局中的settingsTabFragment?

我需要添加设置到我的最后一个选项卡,并有数据可编辑,但我有困难这样做,不知道该使用哪些方法

我遵循的教程:https://github.com/codepath/安卓_guides/wiki/Settings-with-PreferenceFragment

设置StabFragment。爪哇

package com.example.sachin.pedometer;

import 安卓.content.Context;
import 安卓.net.Uri;
import 安卓.os.Bundle;
import 安卓.preference.ListPreference;
import 安卓.preference.Preference;
import 安卓.support.annotation.Nullable;
import 安卓.support.v4.app.Fragment;
import 安卓.view.LayoutInflater;
import 安卓.view.View;
import 安卓.view.ViewGroup;

import com.takisoft.fix.support.v7.preference.PreferenceFragmentCompat;


/**
 * A simple {@link Fragment} subclass.
 * Activities that contain this fragment must implement the
 * {@link SettingsTabFragment.OnFragmentInteractionListener} interface
 * to handle interaction events.
 * Use the {@link SettingsTabFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class SettingsTabFragment extends PreferenceFragmentCompat {

    private ListPreference mListPreference;

    @Override
    public void onCreatePreferencesFix(@Nullable Bundle savedInstanceState, String rootKey) {

        // Indicate here the XML resource you created above that holds the preferences
        setPreferencesFromResource(R.xml.preferences, rootKey);
    }

    /*@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        mListPreference = (ListPreference)  getPreferenceManager().findPreference("preference_key");
    mListPreference.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
            @Override
            public boolean onPreferenceChange(Preference preference, Object newValue) {
                // your code here
                return view;
            }
        }

        return inflater.inflate(R.layout.fragment_settings_tab, container, false);
    }
}

共 (1) 个答案

  1. # 1 楼答案

    我真的不知道你的问题是什么!您是否未能实现ListPreference?当您尝试运行时,应用程序是否崩溃?如果是,请提供logcat

    同时,试试这个

    在你的

    preference.xml

    加上这个

    <PreferenceScreen
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:title="@string/root_title">
    
    <ListPreference
            android:key="list_preference"
            android:title="@string/title_list_preference"
            android:summary="@string/summary_list_preference"
            android:entries="@array/entries_list_preference"
            android:entryValues="@array/entryvalues_list_preference"
            android:dialogTitle="@string/dialog_title_list_preference" />
    
    
    </PreferenceScreen>
    

    在你的

    SettingsTabFragment.java

    加上这个

    public static class SettingsTabFragment extends PreferenceFragmentCompat 
    implements SharedPreferences.OnSharedPreferenceChangeListener{
    
    private ListPreference mListPreference;
    
        @Override
        public void onCreatePreferences(Bundle savedInstanceState, String 
        rootKey) {
        // Load the preferences from an XML resource
        setPreferencesFromResource(R.xml.preferences, rootKey);
        }
    
       @Override
       public void onSharedPreferenceChanged(SharedPreferences 
       sharedPreferences, String key) {
    
        setPreferenceSummery();
    
        }
    private void setPreferenceSummery(Preference preference,Object value){
    
    String stringValue = value.toString();
    
    if (preference instanceof ListPreference){
    // For list preferences, look up the correct display value in
    // the preference's 'entries' list (since they have separate labels/values).
    mListPreference = (ListPreference) preference;
    
    int prefIndex = listPreference.findIndexOfValue(stringValue);
    //same code in one line
    //int prefIndex = ((ListPreference) preference).findIndexOfValue(value);
    
    //prefIndex must be is equal or garter than zero because
    //array count as 0 to ....
    if (prefIndex >= 0){
    mListPreference.setSummary(mListPreference.getEntries()[prefIndex]);
     }
    } else {
    // For other preferences, set the summary to the value's simple string 
    //representation.
    preference.setSummary(stringValue);
       }
    }
    
    }